﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SageKing.Core.Extensions;
using SageKing.Core.Options;

namespace SageKing.Database.EF.Contracts;

/// <summary>
/// 模板实体基类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Tt"></typeparam>
public class EntityTemplateConfig<T, Tt> : EntityBase<T>, IEntityTemplateConfig<T, Tt>
{
    public virtual Tt? TemplateId { get; set; }
    public virtual TemplateSourceEnum SourceType { get; set; }
    public virtual string KKey { get; set; }
    public virtual string VValue { get; set; }
    public virtual string Code { get; set; }
    public virtual bool? Enable { get; set; } = false;
    public virtual string Description { get; set; }
    

    #region 排序
    public virtual int? SortNo { get; set; } = 0;

    public virtual int? OrderNo { get; set; } = 0;
    #endregion

    #region JsonColumn

    public virtual string JsonDicColumn { get; set; }

    private Dictionary<string, JsonColumnPropertiesBase> _JsonDicColumnDic;
    /// <summary>
    /// 此属性不会被映射到数据库，用于操作反序列化后的 JSON 对象
    /// </summary>
    [NotMapped]
    public Dictionary<string, JsonColumnPropertiesBase> JsonDicColumnDic
    {
        get
        {
            if (_JsonDicColumnDic != null) return _JsonDicColumnDic;
            _JsonDicColumnDic = JsonDicColumn.IsNotNullOrWhiteSpace() ? JsonDicColumn.JsonTo<Dictionary<string, JsonColumnPropertiesBase>>() : null;
            return _JsonDicColumnDic ??= new Dictionary<string, JsonColumnPropertiesBase>();
        }
        set
        {
            _JsonDicColumnDic = value;
            JsonDicColumn = value.toJsonStr();
        }
    }
    #endregion
}
